#include "AlgorithmLink_lines.h"
#include "../../../src_algorithms/src_slide/slide.h"

using namespace std;

//1.1 单根线段推挤
void AlgorithmLink_lines::pushLineRunBegin(LineUI& line, vector<LineUI>& lines) {
	m_selectLine = line;
	m_beginLines.clear();
	for (auto& line : lines) {
		double x1 = (double)line.x1(), y1 = (double)line.y1();
		double x2 = (double)line.x2(), y2 = (double)line.y2();
		m_beginLines.emplace_back(Line(Point(x1, y1), Point(x2, y2)));
	}
	if (m_beginLines.empty()) return;
	m_beginLines[0].Pt1.locked = true;
	m_beginLines.back().Pt2.locked = true;
	m_ui_wires = &lines;
}
bool AlgorithmLink_lines::pushLineRun(const QPointF& offset, const bool inGreed, const bool postOn) {
	if (!m_data) {
		cout << "pushLineRun: m_data is nullptr" << endl;
		return false;
	}
	//1.定义算法的输入数据
	vector<Line> polyLines_input;
	Line line_input;
	Point offset_input;
	//3.设置算法输入数据
	bool res = setPushLineData(polyLines_input, line_input, offset_input, offset);
	if (!res) return false;
	//4.创建推挤对象,设置基本参数
	SlideLine slide;
	int mode = 0;
	slide.setMode(mode);
	slide.setInGreed(inGreed);
	//5.执行算法
	int result = slide.pushSingleLine(polyLines_input, line_input, offset_input, postOn);
	//6.反馈给前端
	if (result == 0) {
		pushLineDataUpdate(polyLines_input);
		return true;
	}
	else {
		cout << "pushLineRun: pushSingleLine failed, error code= " << result << " ###" << endl;
		return false;
	}
	return true;
}

//1.3 Pin推挤pushOnePt
void AlgorithmLink_lines::pushPinRunBegin(QPointF pt, vector<LineUI>& lines) {
	m_selectPt = pt;
	m_beginLines.clear();
	for (auto& line : lines) {
		double x1 = (double)line.x1(), y1 = (double)line.y1();
		double x2 = (double)line.x2(), y2 = (double)line.y2();
		m_beginLines.emplace_back(Line(Point(x1, y1), Point(x2, y2)));
	}
	m_ui_wires = &lines;
}
bool AlgorithmLink_lines::pushPinRun(const QPointF& offset, bool inGreed) {
	if (!m_data) {
		cout << "pushPinRun: m_data is nullptr" << endl;
		return false;
	}
	//1.定义算法的输入数据
	vector<Line> polyLines_input;
	Point pt;
	Point offset_input;
	//3.设置算法输入数据
	bool res = setPushPinData(polyLines_input, pt, offset_input, offset);

	//4.创建推挤对象,设置基本参数m_config->m_onGrids, m_config->m_postOn
	SlideLine slide;
	int mode = 1;
	slide.setMode(mode);
	slide.setInGreed(inGreed);

	//5.执行算法
	int result = slide.pushOnePt(polyLines_input, pt, offset_input);

	//6.反馈给前端
	if (result == 0) {	//推中间点
		pushLineDataUpdate(polyLines_input);
		return true;
	}
	else if (result == 1) {	//推Pin,清空布线信息
		pushLineDataUpdate(polyLines_input);
		m_data->m_wires.clear();
		return true;
	}
	else {
		cout << "pushLineRun: pushSingleLine failed, error code= " << result << " ###" << endl;
		return false;
	}
	return true;

}

//私有函数
bool AlgorithmLink_lines::setPushLineData(vector<Line>& polyLines_input, Line& line_input, Point& offset_input, const QPointF& offset_ui) {
	bool zeroOffset = offset_ui.x() == 0 && offset_ui.y() == 0;
	if (zeroOffset || m_beginLines.empty()) return false;	//偏移量为0 或 输入数据为空，结束算法
	if (m_selectLine.length() == 0) return false;	//选取的线段为零线段，结束算法

	polyLines_input = vector<Line>(m_beginLines);
	line_input = Line(Point(m_selectLine.x1(), m_selectLine.y1()), Point(m_selectLine.x2(), m_selectLine.y2()));
	offset_input = Point(offset_ui.x(), offset_ui.y());
	return true;
}
void AlgorithmLink_lines::pushLineDataUpdate(vector<Line>& polyLines_input) {
	m_ui_wires->clear();
	for (auto& line : polyLines_input) {
		m_ui_wires->emplace_back(LineUI(line.Pt1.x, line.Pt1.y, line.Pt2.x, line.Pt2.y));
	}
}
bool AlgorithmLink_lines::setPushPinData(vector<Line>& polyLines_input, Point& pt_input, Point& offset_input, const QPointF& offset_ui) {
	polyLines_input = vector<Line>(m_beginLines);
	pt_input = Point(m_selectPt.x(), m_selectPt.y());
	offset_input = Point(offset_ui.x(), offset_ui.y());
	return true;
}
